package com.wyd.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyd.api.TcProductService;
import com.wyd.common.IDCreator;
import com.wyd.common.Result;
import com.wyd.core.common.AppUser;
import com.wyd.entity.TcProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


@Slf4j
@RestController
@RequestMapping("/product")
@Api(value = "产品", tags = "产品")
public class TcProductController {

	@Autowired
	private TcProductService tcProductService;
	 
	/**
	 * 获取产品列表
	 * @param request
	 * @param response
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value = "获取产品列表", notes = "获取产品列表")
	@RequestMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.GET,RequestMethod.POST})
	public ResponseEntity<Result> list(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "分页参数，从1开始") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@ApiParam(value = "分页用") @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@ApiParam(value = "产品标题") @RequestParam(value = "articleTitle", required = false) String articleTitle,
			@ModelAttribute AppUser user) {
		
		TcProduct data = new TcProduct();

		log.info("======获取产品列表==========");
		PageInfo<TcProduct> list = tcProductService.selectPageList(data, page, limit);
		
		return new ResponseEntity<Result>(Result.success(list), HttpStatus.OK);
	}
	
	/**
	 * 添加产品
	 * @return
	 */
	@ApiOperation(value = "添加产品", notes = "添加产品")
	@RequestMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.GET,RequestMethod.POST})
	public synchronized ResponseEntity<Result> add(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TcProduct  data,
			@ModelAttribute AppUser user) {
		
		log.info("======添加产品======data="+JSON.toJSONString(data));

		data.setId(new IDCreator().nextId());
		data.setCreateUser(user.getUsername());
		data.setModifyUser(user.getUsername());
		int count =tcProductService.save(data);

		return new ResponseEntity<Result>(Result.success(String.valueOf(data.getId())), HttpStatus.OK);
	}
	
	
	/**
	 * 修改产品
	 * @return
	 */
	@ApiOperation(value = "修改产品", notes = "修改产品")
	@RequestMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.GET,RequestMethod.POST})
	public ResponseEntity<Result> update(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TcProduct  data,
			@ModelAttribute AppUser  user) {
		
		data.setModifyUser(user.getUsername());
		int count =tcProductService.updateNotNull(data);
		
		return new ResponseEntity<Result>(Result.success(String.valueOf(count)), HttpStatus.OK);
	}
	
	/**
	 * 删除产品
	 * @return
	 */
	@RequestMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.GET,RequestMethod.POST})
	public ResponseEntity<Result> delete(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "id") @RequestParam(value = "id", required = true) String id,
			@ModelAttribute AppUser user) {
		log.info("======删除产品接口==========id="+id);
		
		String [] idArr = id.split(",");
		int count=0;
		for(int i=0;i<idArr.length;i++){
			count = tcProductService.delete(idArr[i]);
		}
		return new ResponseEntity<Result>(Result.success(String.valueOf(count)), HttpStatus.OK);
	}
	
	/**
	 * 获取产品详情
	 * @param request
	 * @param response
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value = "获取产品详情", notes = "获取产品详情")
	@GetMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Result> detail(@ApiParam(value = "id") @RequestParam(value = "id", required = true) String id) {
		
		log.info("======获取产品详情======id="+id);
		
		TcProduct data = tcProductService.selectByKey(id);

		return new ResponseEntity<Result>(Result.success(data), HttpStatus.OK);
	}
	
	/**
	 * 删除全部产品
	 * @return
	 */
	@RequestMapping(value = "/deleteAll", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.GET,RequestMethod.POST})
	public ResponseEntity<Result> deleteAll(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute AppUser user) {
		log.info("======删除全部产品接口==========userId="+user.getUserId());
		
		TcProduct data = new TcProduct();
		List<TcProduct> list = tcProductService.selectByExample(data);
		if(list != null && list.size() >0){
			for(TcProduct footprint:list){
				tcProductService.delete(footprint.getId());
			}
		}
		
		return new ResponseEntity<Result>(Result.success(String.valueOf(list.size())), HttpStatus.OK);
	}
	
	
}







